﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Modules.Core;
using System.Data;
using Modules.Library.Entities;
using System.Windows;
using System.Windows.Input;
using System.Collections;

namespace ComputerStoreManager.ViewModel {
    public class ViewEmployeeViewModel : BasePersonControlViewModel {

        #region Variables
        private Button btnPreview;
        private Button btnNext;
        private DataGrid dataGrid1;
        private Button btnShowAll;
        private Button btnSearch;
        private ComboBox cbbSearchBy;
        private TextBox txtSearch;
        private Button btnChangeProfileInfo;
        private ComboBox cbbTypeName;
        private TextBox txtSex;
        private DataTable dataTemp;
        private int counter;
        private Hashtable loaiNhanVien;

        #endregion

        #region ICommand
        public ICommand PreviewCommand {
            get;
            private set;
        }
        public ICommand NextCommand {
            get;
            private set;
        }
        public ICommand DoChangeEmployeeInfoCommand {
            get;
            private set;
        }
        public ICommand SearchCommand {
            get;
            private set;
        }
        public ICommand ShowAllCommand {
            get;
            private set;
        }
        public ICommand SelectionChangedCommand {
            get;
            private set;
        }
        #endregion

        #region InitCmd
        private void InitCmd() {
            PreviewCommand = new ExecutableCommand(DoPreview);
            NextCommand = new ExecutableCommand(DoNext);
            DoChangeEmployeeInfoCommand = new ExecutableCommand(DoChangeEmployeeInfo);
            SearchCommand = new ExecutableCommand(DoSearch);
            ShowAllCommand = new ExecutableCommand(DoShowAll);
            SelectionChangedCommand = new ExecutableCommand(SelectionChangedEvent);
        }

        public override void InitControlInfo() {
            try {
                loaiNhanVien = Model.GetLoaiNhanVienInfo();

                foreach (DictionaryEntry entry in loaiNhanVien) {
                    ComboBoxItem item = new ComboBoxItem();
                    item.Content = entry.Value;
                    item.Tag = entry.Key;
                    cbbTypeName.Items.Add(item);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        #endregion

        #region Constructor
        public ViewEmployeeViewModel(Button btnPreview, Button btnNext, DataGrid dataGrid1, Button btnShowAll, Button btnSearch, ComboBox cbbSearchBy, TextBox txtSearch,
            Button btnChangeProfileInfo, TextBox txtUserName, ComboBox cbbTypeName, TextBox txtEmail, TextBox txtPhone, TextBox txtIdentifier, TextBox txtSex, TextBox txtBirthDay,
            TextBox txtFullName, TextBox txtEmployeeId)
            : base(txtFullName, txtEmployeeId, txtBirthDay, txtIdentifier, txtPhone, null, txtEmail, txtUserName, null) {

            this.btnPreview = btnPreview;
            this.btnNext = btnNext;
            this.dataGrid1 = dataGrid1;
            this.btnShowAll = btnShowAll;
            this.btnSearch = btnSearch;
            this.cbbSearchBy = cbbSearchBy;
            this.txtSearch = txtSearch;
            this.btnChangeProfileInfo = btnChangeProfileInfo;
            this.cbbTypeName = cbbTypeName;
            this.txtSex = txtSex;

            InitControlInfo();
            InitCmd();
            LoadData();
        }
        #endregion

        #region Methods

        public override bool CheckIsNotNull() {
            //return TxtAddress.Text != " " &&
            //TxtPersonId.Text != " " &&
            return
            TxtPersonFullName.Text != " " &&
            TxtUserName.Text != " " &&
            TxtDateOfBirth.Text != " " &&
            TxtPhone.Text != " " &&
            TxtEmail.Text != " " &&
            TxtIdentifier.Text != " ";

        }

        public void ReloadData() {
            LoadData();
        }

        private void DoChangeEmployeeInfo() {
            try {
                if (CheckIsNotNull()) {
                    Employee emp = new Employee();

                    emp.Email = base.TxtEmail.Text;
                    emp.HoVaTen = base.TxtPersonFullName.Text;
                    emp.MaNhanVien = base.TxtPersonId.Text;
                    emp.UserName = base.TxtUserName.Text;
                    emp.SoCMND = base.TxtIdentifier.Text;
                    emp.GioiTinh = txtSex.Text;
                    //emp.AnhDaiDien = "";
                    emp.SoDienThoai = base.TxtPhone.Text;
                    //emp.DiaChi = ??// cái này còn thiếu
                    emp.MaLoaiNhanVien = ((ComboBoxItem)cbbTypeName.SelectedItem).Tag.ToString();

                    emp.NgaySinh = DateTime.Parse(Converter.ConvertToDateTime(base.TxtDateOfBirth.Text, Converter.MMDDYYYY));
                    emp.AnhDaiDien = "Chưa cập nhật";

                    if (!Model.UpdateData(emp, KindOfObject.BY_EMPLOYEE)) {
                        MessageBox.Show(Model.Error);
                    }
                    else {
                        ReloadData();
                        MessageBox.Show(Constants.SUCCESS);
                    }
                }
                else {
                    MessageBox.Show("Bạn không được bỏ trống bất kỳ trường nào.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoSearch() {
            try {
                var data = Model.SearchInfo(txtSearch.Text, (SearchingCondition)cbbSearchBy.SelectedIndex, KindOfObject.BY_EMPLOYEE);
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác" + ex.Message, "Thong Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoShowAll() {
            try {
                var data = Model.GetListNhanVien();
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác" + ex.Message, "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoNext() {
            if (counter < dataTemp.Rows.Count - 1) {
                counter++;
            }
            LoadPersonInfoByCounter(counter);
        }

        private void DoPreview() {
            if (counter > 0) {
                counter--;
            }
            LoadPersonInfoByCounter(counter);
        }

        private void LoadData() {
            try {
                dataTemp = Model.GetListNhanVien();
                counter = 0;
                if (dataTemp != null && dataTemp.Rows.Count > 0) {
                    LoadPersonInfoByCounter(counter);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }

        private void SelectionChangedEvent(object obj) {
            try {
                if (dataTemp != null && dataTemp.Rows.Count > 0 && dataGrid1.SelectedIndex >= 0) {
                    LoadPersonInfoByCounter(counter = dataGrid1.SelectedIndex);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void LoadPersonInfoByCounter(int counter) {
            try {
                base.TxtDateOfBirth.Text = DateTime.Parse(dataTemp.Rows[counter]["NgaySinh"].ToString()).ToString("dd/MM/yyyy");
                base.TxtEmail.Text = dataTemp.Rows[counter]["Email"].ToString();
                base.TxtPersonId.Text = dataTemp.Rows[counter]["MaNV"].ToString();
                base.TxtPersonFullName.Text = dataTemp.Rows[counter]["HoVaTen"].ToString();
                base.TxtIdentifier.Text = dataTemp.Rows[counter]["CMND"].ToString();
                base.TxtPhone.Text = dataTemp.Rows[counter]["SoDienThoai"].ToString();
                base.TxtUserName.Text = dataTemp.Rows[counter]["UserName"].ToString();
                cbbTypeName.SelectedIndex = ConvertToComboBoxIndex(dataTemp.Rows[counter]["MaLoaiNV"].ToString());
                txtSex.Text = dataTemp.Rows[counter]["GioiTinh"].ToString();
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác" + ex.Message, "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private int ConvertToComboBoxIndex(string maLoaiNV) {
            try {
                int count = -1;
                foreach (DictionaryEntry entry in loaiNhanVien) {
                    count++;
                    if (entry.Key.Equals(maLoaiNV)) {
                        return count;
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            return 0;
        }

        #endregion
    }
}
